package com.dlc.commonlibrary.ble;

import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Handler;
import android.os.Message;


import com.dlc.commonlibrary.R;
import com.dlc.commonlibrary.utils.LogPlus;
import com.dlc.commonlibrary.utils.Util;
import com.seuic.uhf.EPC;
import com.seuic.uhf.UHFService;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by Administrator on 2017/7/25.
 */

public class UHFR2000Helper {
    public static BleCallBack callBack;
    public static UHFR2000Helper mInstance;

    private UHFService mDevice;
    private List<EPC> mEPCList;
    private InventoryRunable mInventoryRunable;
    public boolean mInventoryStart = false;
    private Thread mInventoryThread;
    private int m_count = 0;


    private static SoundPool mSoundPool;
    private UHFR2000Helper() {
        mInstance = this;
    }

    public static UHFR2000Helper getInstance(){
        if(mInstance == null){
            mInstance = new UHFR2000Helper();
        }
        return mInstance;
    }


    /**
     * 蓝牙获取数据回调
     */
    public static  interface  BleCallBack{
        public void readDatas(List<EPC> data);
        public void readNums(int nums);
    }


    private void playSound() {
//		if (mSoundPool == null) {
//			mSoundPool = new SoundPool(3, AudioManager.STREAM_MUSIC, 20);
//			soundID = mSoundPool.load(getContext(), R.raw.scan, 1);// "/system/media/audio/notifications/Antimony.ogg"
//		}
//		mSoundPool.play(soundID, 1, 1, 0, 0, 1);
        if (!Util.soundfinished) {
            Util.play(1, 0);
        }
    }
    public void readData(BleCallBack c){
        callBack = c;

        m_count = 0;
        try {
            mDevice = UHFService.getInstance();
        }catch (Exception e){}

        mEPCList = new ArrayList<EPC>();
        mInventoryRunable = new InventoryRunable();
        if (mInventoryThread != null && mInventoryThread.isAlive()) {
            System.out.println("Thread not null");
            return;
        }
        if (mDevice != null && mDevice.inventoryStart()) {
            mInventoryStart = true;
            mInventoryThread = new Thread(mInventoryRunable);
            mInventoryThread.start();
        }

    }

    public void stopRead(){

        if (callBack != null){
            callBack.readDatas(mEPCList);
        }
        mInventoryStart = false;

        if (mInventoryThread != null) {
            mInventoryThread.interrupt();
            mInventoryThread = null;
        }
        System.out.println("begin stop!!");
        if (mDevice != null && mDevice.inventoryStop()) {
            System.out.println("end stop!!");

        } else {
            System.out.println("RfidInventoryStop faild.");
        }
    }

    private class InventoryRunable implements Runnable {
        @Override
        public void run() {
            while (mInventoryStart) {
                mEPCList = mDevice.getTagIDs();
                if (callBack != null && mEPCList != null){
                    callBack.readNums(mEPCList.size());
                }
                refreshData();
                //LogPlus.e("mEPCList="+mEPCList.toString());
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
    }

    private void refreshData() {

        if (mEPCList != null) {
            // Gets the number inside the list of all labels
            int count = 0;
            for (EPC item : mEPCList) {
                count += item.count;
            }
            if (count > m_count) {
                playSound();
            }
            m_count = count;
        }
    }

}
